<template>
    <el-container class="applet">
        <el-main class="wrapper">
            <section class="panel">
                <div class="panel-body cms-content">
                    <div class="btn-group" style="margin-bottom: 15px">
                        <el-button @click="list" type="primary" icon="fa fa-refresh"
                                   size="small"> 刷新
                        </el-button>
                        <el-button @click="create" type="primary" icon="fa fa-plus"
                                   size="small"> 新增
                        </el-button>
                    </div>
                    <el-table v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              tooltip-effect="dark" :data="listData"  border>
                        <el-table-column prop="id" label="ID" width="50"></el-table-column>
                        <el-table-column prop="name" label="标识" width="150"></el-table-column>
                        <el-table-column prop="title" label="名称" width="200"></el-table-column>
                        <el-table-column prop="value" label="值"  width="500"></el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="edit(scope.row)" icon="fa fa-edit" plain>
                                    编辑
                                </el-button>
                                <el-button icon="fa fa-trash" @click="destory(scope.row.id)" type="danger" size="mini" plain> 删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </section>
        </el-main>
        <!-- 新增用户 -->
        <el-dialog
                v-if="dialog.show"
                :title="dialog.title"
                custom-class="cms-dialog"
                :top="dialog.top"
                :visible.sync="dialog.show"
                :width="dialog.width">
            <el-form :model="form" size="medium" :status-icon="true" ref="form"
                     label-width="80px" class="demo-ruleForm">
                <el-form-item></el-form-item>
                <el-form-item required label="标识：" :rules="[{ required: true, message: '请填写标识', trigger: 'blur'}]" prop="name">
                    <el-input v-model="form.name" placeholder="请填写标识"></el-input>
                </el-form-item>
                <el-form-item required label="名称：" :rules="[{ required: true, message: '请填写名称', trigger: 'blur'}]" prop="title">
                    <el-input v-model="form.title" placeholder="请填写名称"></el-input>
                </el-form-item>
                <el-form-item required label="值：" :rules="[{ required: true, message: '请填写值', trigger: 'blur'}]" prop="value">
                    <el-input v-model="form.value" placeholder="请填写值"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                    <el-button @click="dialog.show = false">取 消</el-button>
                    <el-button type="primary" @click="update('form')">确 定</el-button>
              </span>
        </el-dialog>
    </el-container>
</template>

<script>
    import {Config, ConfigUpdate,ConfigDestory} from '@/utils/request';
    import {mapGetters} from 'vuex';
    export default {
        data() {
            return {
                listData: [],
                dialog: {
                    title: '',
                    top: '5vh',
                    show: false,
                    width: '550px'
                },
                form:null,
                applet: 'binggege'
            }
        },
        activated() {
            this.$store.dispatch('setBingGeGeActive', '/binggege/config');
            this.list();
        },
        computed: {
            ...mapGetters(['loading']),
        },
        methods: {
            //获取列表
            list() {
                this.$store.dispatch('setLoading', true);
                Config({params: {applet: this.applet}}).then((response) => {
                    if (response.code) {
                        this.$message.error(response.msg);
                    } else {
                        this.$store.dispatch('setLoading', false);
                        this.listData = response.data;
                    }
                })
            },
            // 新增
            create() {
                this.form = {
                    id: '',
                    name: '',
                    title: '',
                    value: '',
                    applet:this.applet
                };
                this.dialog.title = '新增配置';
                this.dialog.show = true;
            },
            //编辑
            edit(info) {
                this.form = {
                    id: info.id,
                    name: info.name,
                    title: info.title,
                    value: info.value,
                    applet:this.applet
                };
                this.dialog.title = '编辑配置';
                this.dialog.show = true;
            },
            //更新
            update(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        ConfigUpdate(this.form).then((res) => {
                            if (res.code) {
                                this.$message.error(res.msg);
                            } else {
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: res.msg,
                                    onClose: () => {
                                        this.dialog.show = false;
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            //删除
            destory(id){
                this.$confirm('确定要删除该信息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    ConfigDestory({id: id,applet:this.applet}).then((response) => {
                        if (response.code) {
                            this.$message.error(response.msg);
                        } else {
                            this.list();
                        }
                    });
                }).catch(() => {});
            },

        },
    }
</script>
